﻿using System;
using System.Text;

namespace MySql.Simple.Mapper.Query
{
    public class WhereClause
    {
        private readonly StringBuilder sb = new StringBuilder();

        private void AppendValue(object value)
        {
            if (value.GetType() == typeof(string))
            {
                sb.Append("'");
                sb.Append(value);
                sb.Append("'");
            }
            else if (value.GetType() == typeof(DateTime))
            {
                sb.Append("'");
                sb.Append(((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"));
                sb.Append("'");                
            }
            else
            {
                sb.Append(value);                
            }
        }

        public WhereClause(string name, object value)
        {
            sb.Append("WHERE ");

            sb.Append(name);
            sb.Append(" = ");
            AppendValue(value);
        }

        public WhereClause And(string name, object value)
        {
            sb.Append(" AND ");

            sb.Append(name);
            sb.Append(" = ");
            AppendValue(value);

            return this;
        }

        public override string ToString()
        {
            return sb.ToString();
        }
    }
}